package com.moli.iov.open.web.controller;

import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.open.model.vo.req.UserVo;
import com.moli.iov.open.service.IUserFacadeService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.model.dto.res.UserRoleDto;
import com.moli.iov.sys.model.po.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户业务 第三方调用
 * </p>
 *
 * @author zql
 * @since 2019/9/21 15:16
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

  @Autowired
  private IUserFacadeService userFacadeService;

  /**
   * 添加用户
   * @param userVo
   * @return
   */
  @RequestMapping("/saveUser")
  public RestResponse saveUser(@RequestBody @Validated({UserVo.Save.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.saveUser(userVo);
  }


  /**
   * 分页查询用户列表
   * @param userVo
   * @return
   */
  @RequestMapping("/list")
  public RestResponse<PageExtend<User>> listUserOfPage(@RequestBody(required = false) UserVo userVo){
    if(userVo == null){
      userVo = new UserVo();
    }
    assembleUserInfo(userVo);
    return userFacadeService.listUserOfPage(userVo);
  }

  /**
   * 修改用户
   * @param userVo
   * @return
   */
  @RequestMapping("/update")
  public RestResponse updateUser(@RequestBody @Validated({UserVo.Update.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.updateUser(userVo);
  }

  /**
   * 修改密码 / 重置密码
   * @param userVo
   * @return
   */
  @RequestMapping("/updatePwd")
  public RestResponse updatePwd(@RequestBody @Validated({UserVo.UpdatePwd.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.updatePwd(userVo);
  }

  /**
   * 查询用户角色列表
   * @param userVo
   * @return
   */
  @RequestMapping("/getUserRoleByAccount")
  public RestResponse<UserRoleDto> getUserRoleByAccount(@RequestBody @Validated({UserVo.GetUserRoleByAccount.class}) UserVo userVo){
    assembleUserInfo(userVo);
    return userFacadeService.getUserRoleByAccount(userVo);
  }
}
